#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

struct person
{
    char name[ 20 ];
    int time;
};

bool operator < ( const person p1, const person p2 )
{
    if ( p1.time < p2.time )
        return true;
    if ( p1.time == p2.time )
        if ( strcmp( p1.name, p2.name ) < 0 )
            return true;
    return false;
}

person p[ 21 ];
int n;

void init( )
{
    int i, t1, t2;
    char tmp;
    for ( i = 0; i < n; i++ )
    {
        scanf("%s%d%c%d", p[ i ].name, &t1, &tmp, &t2);
        p[ i ].time = t1 * 100 + t2;
    }
}

void sort( )
{
    int i, j;
    person t;
    for ( i = 0; i < n - 1; i++ )
        for ( j = i + 1; j < n; j++ )
            if ( p[ j ] < p[ i ] )
            {
                t = p[ j ];
                p[ j ] = p[ i ];
                p[ i ] = t;
            }
}

void print( )
{
    int i, t = 1;
    for ( i = 0; i < n; i++ )
    {
        if ( i == 0 )
            printf("%s 1\n", p[ i ].name);
        else
        {
            if ( p[ i ].time == p[ i - 1 ].time )
                printf("%s %d\n", p[ i ].name, t);
            else
            {
                t = i + 1;
                printf("%s %d\n", p[ i ].name, t);
            }
        }
    }
}

int main( )
{
    int t = 0;
    bool blank = false;
    while ( scanf("%d", &n) && n != 0 )
    {
        if ( blank )
            printf("\n");
        else
            blank = true;
        t++;
        init( );
        sort( );
        printf("Case #%d\n", t);
        print( );
    }
    return 0;
}
